package com.czh.intelligentoj.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czh.intelligentoj.common.BaseResponse;
import com.czh.intelligentoj.common.ErrorCode;
import com.czh.intelligentoj.common.ResultUtils;
import com.czh.intelligentoj.exception.BusinessException;
import com.czh.intelligentoj.judge.JudgeService;
import com.czh.intelligentoj.model.dto.questionsubmit.QuestionSubmitAddRequest;
import com.czh.intelligentoj.model.dto.questionsubmit.QuestionSubmitQueryRequest;
import com.czh.intelligentoj.model.entity.QuestionSubmit;
import com.czh.intelligentoj.model.entity.User;
import com.czh.intelligentoj.model.vo.QuestionSubmitVO;
import com.czh.intelligentoj.service.QuestionSubmitService;
import com.czh.intelligentoj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 题目提交接口
 *
 * @author CZH
 * 
 */

@RestController
@RequestMapping("/question_submit")
@Slf4j
public class QuestionSubmitController {

    @Resource
    private QuestionSubmitService questionSubmitService;

    @Resource
    private UserService userService;

//    @Resource
//    private JudgeService judgeService;

    /**
     * 题目提交请求接口
     *
     * @param questionSubmitAddRequest
     * @param request
     * @return
     *       提交记录的id
     */
    @PostMapping("/")
    public BaseResponse<Long> doQuestionSubmit(@RequestBody  QuestionSubmitAddRequest questionSubmitAddRequest,
                                               HttpServletRequest request) {
        if (questionSubmitAddRequest == null || questionSubmitAddRequest.getQuestionId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能提交
        final User loginUser = userService.getLoginUser(request);
        Long result = questionSubmitService.doQuestionSubmit(questionSubmitAddRequest, loginUser);
        return ResultUtils.success(result);
    }

    /**
     *  查询题目提交记录列表接口(仅自己和管理员可见)
     * @param questionSubmitQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<QuestionSubmitVO>> listQuestionSubmitpage(@RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest,
                                                                      HttpServletRequest request) {
        int current = questionSubmitQueryRequest.getCurrent();
        int pageSize = questionSubmitQueryRequest.getPageSize();
        Page<QuestionSubmit> page = questionSubmitService.page(new Page<>(current, pageSize),
                questionSubmitService.getQueryWrapper(questionSubmitQueryRequest));
        final User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(questionSubmitService.getQuestionSubmitVOPage(page, loginUser));
    }

}
